package commands;

import error.Errors;
import file.File;
import file.FileString;

public class Include extends Cmd
{
	CmdType includeType;
	String fileAliasToInclude;

	protected Include(FileString string)
	{
		super(string);
		includeType = CmdType.HZ_INCLUDE;
		String[] strs=text.split(" ");
		if(strs.length<2 || strs.length>3)
		{
			Errors.addError(fileName,fileAlias,fileLine,"Формат комманды: '@include fileAlias [BEFORE|AFTER]', а тут что-то с количеством параметров не то");
			return;
		}
		if(!strs[0].toLowerCase().equals("@include"))
		{
			Errors.addError(fileName,fileAlias,fileLine,"Первое слово в строке не '@include', либо после не стоит пробел");
			return;
		}
		fileAliasToInclude=strs[1];
		if(strs.length==3)
		{
			if(strs[2].toLowerCase().equals("after"))
				includeType=CmdType.INCLUDE_AFTER;
			else if(strs[2].toLowerCase().equals("before"))
				includeType=CmdType.INCLUDE_BEFORE;
			else
			{
				Errors.addError(fileName,fileAlias,fileLine,"3й параметр может быть равен [BEFORE|AFTER]");
				return;
			}
		}
		else
			includeType=CmdType.INCLUDE_BEFORE;
		comment+=fileAliasToInclude+":"+includeType.toString();
	}

	public Include(String includeAlias, CmdType includeType)
	{
		this(new FileString("AutoCss.css", "AUTOCSS.CSS", -1, "@include "+includeAlias+" "+((includeType==CmdType.INCLUDE_BEFORE)?"before":"after")));
	}

	@Override
	public CmdType getType()
	{
		return includeType;
	}

	@Override
	public String toOutput()
	{
		return "@include "+fileAliasToInclude+" "+includeType;
	}
}
